package com.ocom.xcxapi.controller;

import com.ocom.common.constant.WxServiceUrl;
import com.ocom.common.entity.human.EntityRoommateInfo;
import com.ocom.common.request.videocall.request.SaveAccreditRequest;
import com.ocom.common.request.videocall.request.SelectVideocallGroupRequest;
import com.ocom.common.resp.Result;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.xcxapi.client.HumanClientService;
import com.ocom.xcxapi.client.PayConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@PreAuthorize("hasRole('ROLE_XCX')")
public class VideoCallController {

    @Autowired
    private PayConfigClientService payConfigClientService;
    @Autowired
    private HumanClientService humanClientService;

    @PostMapping(WxServiceUrl.URL_selectVideocallGroup)
    public Result selectVideocallGroup(@RequestBody SelectVideocallGroupRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        request.setComId(yoCiUser.getComId());
        request.setOpenid(yoCiUser.getUsername());
        return payConfigClientService.selectVideocallGroup(request);
    }

    @PostMapping(WxServiceUrl.URL_saveAccredit)
    public Result saveAccredit(@RequestBody SaveAccreditRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        request.setComId(yoCiUser.getComId());
        request.setOpenid(yoCiUser.getUsername());
        return payConfigClientService.saveAccredit(request);
    }

    @PostMapping(WxServiceUrl.URL_addRoomPer)
    public Result addRoomPer(@RequestBody SaveAccreditRequest request) {
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        request.setComId(yoCiUser.getComId());
        request.setOpenid(yoCiUser.getUsername());

        EntityRoommateInfo entityRoommateInfo = new EntityRoommateInfo();
        entityRoommateInfo.setComId(request.getComId());
        entityRoommateInfo.setPId(request.getPId());
        entityRoommateInfo.setCallWay("1,2");
        entityRoommateInfo.setOpenid(request.getOpenid());
        entityRoommateInfo.setName(request.getPName());

        return humanClientService.AddRoommate(entityRoommateInfo);
    }

}
